package com.atguigu.controller;

import com.atguigu.base.BaseController;
import com.atguigu.entity.Admin;
import com.atguigu.entity.Role;
import com.atguigu.service.AdminService;
import com.atguigu.service.RoleService;
import com.atguigu.util.FileUtil;
import com.atguigu.util.QiniuUtil;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Date:2022/10/31
 * Author:ybc
 * Description:
 */
@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {

    @DubboReference
    private AdminService adminService;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;



    @RequestMapping("/assignRole")
    public String assignRole(@RequestParam("adminId") Long adminId, @RequestParam("roleIds") List<Long> roleIds, Model model){
        adminService.assignRole(adminId, roleIds);
        return successPage(model, "保存角色成功!");
    }


    @RequestMapping("/assignShow/{adminId}")
    public String assignShow(@PathVariable("adminId") Long adminId,Model model){
        model.addAttribute("adminId",adminId);
        Map<String, List<Role>> roleMap = adminService.findRolesByAdminId(adminId);
        model.addAllAttributes(roleMap);
        model.addAttribute("unAssignRoleList",roleMap.get("unAssignRoleList"));
        model.addAttribute("assignRoleList",roleMap.get("assignRoleList"));
        return "admin/assignShow";
    }



    @RequestMapping
    public String findPage(Model model, @RequestParam Map<String, Object> filters){
        PageInfo<Admin> page = adminService.findPage(filters);
        model.addAttribute("page", page);
        model.addAttribute("filters", filters);
        return "admin/index";
    }



    @RequestMapping("/create")
    public String create(){
        return "admin/create";
    }



    @RequestMapping("/save")
    public String save(Admin admin, Model model){
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        adminService.save(admin);
        return successPage(model, "新增用户成功");
    }



    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Model model){
        Admin admin = adminService.getById(id);
        model.addAttribute("admin", admin);
        return "admin/edit";
    }



    @RequestMapping("/update")
    public String update(Admin admin, Model model){
        adminService.update(admin);
        return successPage(model, "修改用户成功");
    }



    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        adminService.delete(id);
        return "redirect:/admin";
    }

    @RequestMapping("/uploadShow/{id}")
    public String uploadShow(@PathVariable Long id, Model model){
        model.addAttribute("id", id);
        return "admin/upload";
    }


    @RequestMapping("/upload/{id}")
    public String upload(MultipartFile file, @PathVariable Long id, Model model) throws IOException {
        //使用UUID作为新的文件名
        String fileName = FileUtil.getUUIDName(file.getOriginalFilename());
        //上传图片
        QiniuUtil.upload2Qiniu(file.getBytes(), fileName);
        //修改该用户的头像图片路径
        Admin admin = new Admin();
        admin.setId(id);
        admin.setHeadUrl("http://rkyesodbp.hn-bkt.clouddn.com/"+fileName);
        adminService.update(admin);
        return successPage(model, "上传头像成功");
    }
}
